/**
 * @file STL_Text.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2020-09-15
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#ifndef __STL_TEXT_H__
#define __STL_TEXT_H__

#include <string>
#include <vector>
#include <map>
#include "STL_Tools.h"
#include <set>

namespace STL
{

    class STL_Text
    {
    public:
        /**
         * @brief 获取格式化的字符串(最大长度16k)
         * 
         * @param format 
         * @param ... 
         * @return std::string 
         */
        static std::string format(const char *format, ...);
        /**
         * @brief 分割字符串
         * 
         * @param str 字符串
         * @param pattern 分隔符
         * @return std::vector<std::string> 
         */
        static std::vector<std::string> split(std::string str, std::string pattern);
        /**
         * @brief 大端读取UInt16（即网络字节序）
         * 
         * @param buffer 
         * @param offset 
         * @return STL::uint16 
         */
        static STL::uint16 readUInt16BE(std::string buffer, size_t offset);
        /**
         * @brief 小端读取UInt16
         * 
         * @param buffer 
         * @param offset 
         * @return STL::uint16
         */
        static STL::uint16 readUInt16LE(std::string buffer, size_t offset);
        static std::string genNumberPassword(int len);
        static std::string genCharacterAndNumberPassword(int len);
        static std::string trim(std::string str, std::string pattern = " ");
        static std::string strRemove(std::string str, char ch);
        static std::string uint16ToNetworkString(STL::uint16 value);
        static std::string uint32ToNetworkString(STL::uint32 value);
        static STL::uint16 networkCharPointToUint16(char *buf, size_t offset);
        static STL::uint32 networkCharPointToUint32(char *buf, size_t offset);
        static STL::uint16 networkStringPointToUint16(std::string buf, size_t offset);
        static STL::uint32 networkStringPointToUint32(std::string buf, size_t offset);
        static bool string_start_with(const std::string &str, std::string prefix);
        static bool string_end_with(const std::string &str, std::string suffix);
        static std::string toUpperStr(std::string str);
        static std::string toLowerStr(std::string str);
        static bool stringCompareIgnoreCase(std::string str1, std::string str2);
        static std::string addChar(int len, std::string value, char c);
        static std::string addZero(int len, std::string value);
        static std::string string_format(const char *format, ...);
        static int getEmptyId(std::set<int> &list)
        {
            int i = 1;
            while (true)
            {
                if (list.count(i) == 0)
                {
                    list.insert(i);
                    return i;
                }
                i++;
            }
        };
        //--------------------------------------- HEX ---------------------------------------
        /**
         * @brief 字符串转HEX字符串
         * 
         * @param str 
         * @return std::string 
         */
        static std::string StringToHexString(std::string str);
        /**
         * @brief HEX字符串转字符串
         * 
         * @param str 
         * @return std::string 
         */
        static std::string HexStringtoString(std::string str);
        /**
         * @brief 输出Hex字符串(大写)
         * 
         * @param buffer 数据
         * @param max <= 0 表示不限制, > 0 限制输出长度
         * @return std::string 
         */
        static std::string toUpperHex(std::string buffer, size_t offset = 0, size_t max = 0);
        /**
         * @brief 输出Hex字符串(小写)
         * 
         * @param buffer 数据
         * @param max <= 0 表示不限制, > 0 限制输出长度
         * @return std::string 
         */
        static std::string toLowerHex(std::string buffer, size_t offset = 0, size_t max = 0);
        /**
         * @brief 输出Hex字符串,最多100字节输出
         * 
         * @param buffer 数据
         * @return std::string 
         */
        static std::string toHex(std::string buffer);
        static std::string toHex(std::string str, size_t offset);

        static std::string addZero(int value, int len)
        {
            if (len > 9)
                len = 9;
            if (len < 1)
                len = 1;
            auto format = STL_Text::string_format("%06d", len);
            return STL_Text::string_format(format.c_str(), value);
        };
    };

} // namespace STL

#endif /* __STL_TEXT_H__ */
